<?php
// +----------------------------------------------------------------------
// | Author: 闪电 <573725206@qq.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Common\Controller\AuthController;

class ModelController extends AuthController {

	//模型列表
	public function index() {
		$list = M(CONTROLLER_NAME)->field(true)->select();
		$this->assign('list', $list);
		//添置按钮
		$array[] = array('title' => '添加模型', 'url' => U('addmodel'), 'class' => 'primary', 'type' => 'button', 'target' => 'myModal');
		$this->assign('add_button', add_button($array));

		$this->display();
	}
	//新增模型
	public function add() {
		if (IS_POST) {
			if (I('post.param')) {
				$m['name'] = I('post.param');
				$not = M(CONTROLLER_NAME)->where($m)->find();
				if (!$not) {
					$data['status'] = 'y';
					$data['info'] = '该模型可用';
				} else {
					$data['status'] = 'n';
					$data['info'] = '该模型已经存在';
				}
				$this->ajaxReturn($data);
			} else {
				$data = I('post.');
				if (!$data['name']) {
					$this->error('非法操作');
				}
				$data['time'] = time();
				$data['model'] = $this->_pin($data['name']);
				if (M(CONTROLLER_NAME)->add($data)) {
					//创建表模型
					D(CONTROLLER_NAME)->_create($data['model']);
					$this->success('新增模型成功');
				} else {
					$this->error('新增模型失败');
				}
			}

		} else {

			$this->display();
		}

	}
	//删除模型
	public function del() {
		$id = I('get.');
		if (!$id) {
			return false;
		}
		$map['id'] = $id['id'];
		$Controller = M(CONTROLLER_NAME);
		$info = $Controller->where($map)->find();
		if ($info) {
			if (M($info['model'])->count() > 1) {
				$this->error('当前模型下还有数据未清空，暂不能删除...');
			}
			$delete = $Controller->where($map)->delete();
			if ($delete) {
				//删除模型
				D(CONTROLLER_NAME)->delmodel($info['model']);
				M('require')->where(array('modelid' => $id['id']))->delete();
				$this->success('删除模型成功!');
			} else {
				$this->error('删除模型失败!');
			}
		} else {
			$this->error('非法操作');
		}

	}
	//字段管理
	public function field() {
		$id = I('get.id');
		if (!$id) {
			return false;
		}
		if (IS_POST) {

		} else {

			//添置按钮
			$array[] = array('title' => '新增字段', 'url' => U('addfield'), 'class' => 'primary', 'type' => 'button', 'target' => 'myModal');
			$this->assign('add_button', add_button($array));

			$list = M('require')->field(true)->where(array('modelid' => $id))->order('sort')->select();
			$this->assign('list', $list);
			$this->display();
		}

	}
	//新增字段
	public function addfield() {

		if (IS_POST) {

			$param = I('post.param');
			$m['id'] = I('get.id');

			if ($param) {

				$r = M(CONTROLLER_NAME)->where($m)->find();
				if ($r) {

					$is_field = D(CONTROLLER_NAME)->is_field($r['model'], $this->_pin($param));

					if ($is_field) {
						$data['info'] = '该字段已经存在';
						$data['status'] = 'n';
					} else {
						$data['info'] = '验证成功';
						$data['status'] = 'y';
					}
					$this->ajaxReturn($data);

				} else {
					$data['info'] = '该字段已经存在';
					$data['status'] = 'n';
					$this->ajaxReturn($data);
				}
			} else {

				$data = I('post.');
				$data['field'] = $this->_pin($data['name']);

				$requires = M('require');
				$requires->create();
				$data['sort'] = $requires->where(array('modelid' => $data['modelid']))->count() + 1;
				$mode = $requires->add($data);

				if ($mode) {
					$m['id'] = $data['modelid'];
					$r = M(CONTROLLER_NAME)->where($m)->find();
					if ($r) {
						//此处错误未修复20160225 573725206@qq.com
						$typearray = explode('|', urldecode($data['type']));
						$type = $typearray['1'];
						if ($type == 'float') {
							if ($data['length'] > 50) {
								$this->error('字段长度超出');
							}
						}
						$create_data = D(CONTROLLER_NAME)->_create_field(urldecode($r['model']), urldecode($data['field']), $data['length'], urldecode($data['name']), $type);

						if (!$create_data) {
							$this->success('新增成功');
						}
						// if ($create_data) {
						// 	$this->success('新增成功');
						// } else {
						// 	//$requires->where(array('id' => $mode))->delete();
						// 	$this->error('写入失败');
						// }
					} else {
						$this->error('未获取到模型表名');
					}

				} else {
					$this->error('新增失败');
				}
			}
		}

	}
	//字段修改
	public function field_rom() {
		$name = I('get.name');
		$val = I('get.val');
		$id = I('get.id');
		$data[$name] = $val;

		if (M('require')->where(array('id' => $id))->save($data)) {
			$json['status'] = true;
			$json['content'] = "成功修改为{$val}";
		} else {
			$json['status'] = false;
			$json['content'] = "修改失败!";
		}
		$this->ajaxReturn($json);

	}
	//删除空格
	private function trimall($str) {
		$qian = array(" ", "　", "\t", "\n", "\r");
		$hou = array("", "", "", "", "");
		return str_replace($qian, $hou, $str);
	}
	private function _pin($string) {
		return $this->trimall(D(CONTROLLER_NAME)->_pinyin($string, 'all'));
	}
	//删除字段
	public function delfield() {
		$id = I('get.id');
		if (!$id) {
			return false;
		}
		$map['id'] = $id;
		$require = M('require');
		$requ = $require->where($map)->find();
		if ($requ) {
			$require->where($map)->delete();
			$model = M(CONTROLLER_NAME)->where(array('id' => $requ['modelid']))->find();
			if ($model) {
				if (D(CONTROLLER_NAME)->del_field($model['model'], $requ['field'])) {
					$this->error('删除失败!');
				} else {
					$this->success('删除字段成功');

				}
			} else {
				$this->error('查找不到对应模型');
			}
		} else {
			$this->error('未知错误');
		}

	}
	//联动模型管理
	public function link() {
		if (IS_GET) {
			$id = I('get.id');
			if ($id) {
				$link = M($this->link)->where(array('linkid' => $id))->field(true)->cache(true, 60)->select();
				$this->ajaxReturn(list_to_tree($link));
			} else {
				//添置按钮
				$array[] = array('title' => '新增联动模型', 'url' => U('addmodel'), 'class' => 'primary', 'type' => 'button', 'target' => 'myModal');
				$this->assign('add_button', add_button($array));
				$this->assign('list', M($this->link_list)->field(true)->select());
				$this->display();
			}
		}

	}
	//新增联动模型
	public function addlink() {
		if (IS_POST) {
			if (I('post.param')) {
				$m['name'] = I('post.param');
				$not = M($this->link_list)->where($m)->find();
				if (!$not) {
					$data['status'] = 'y';
					$data['info'] = '该联动模型可用';
				} else {
					$data['status'] = 'n';
					$data['info'] = '该联动模型已经存在';
				}
				$this->ajaxReturn($data);
			} else {
				$data = I('post.');
				if (!$data['name']) {
					$this->error('非法操作');
				}
				$data['time'] = time();
				//pid存在即添加联系动数据
				if (isset($data['pid'])) {
					if (M($this->link)->add($data)) {
						$this->success('新增成功');
					} else {
						$this->error('新增失败');
					}

				} else {
					$data['field'] = $this->_pin($data['name']);
					if (M($this->link_list)->add($data)) {
						$this->success('新增联动模型成功');
					} else {
						$this->error('新增联动模型失败');
					}
				}

			}
		} else {
			//添置按钮
			$id = I('get.id');
			if (!$id) {
				return false;
			}
			$array[] = array('title' => '添加', 'url' => U('addfield'), 'class' => 'primary', 'type' => 'button', 'target' => 'myModal');
			$this->assign('add_button', add_button($array));
			$link = M($this->link)->field(true)->where(array('linkid' => $id))->select();
			$this->assign('list', list_to_tree($link));
			$title = M($this->link_list)->where(array('id' => $id))->find();
			$this->assign('title', $title);
			$this->display();
		}
	}
	//删除联动模型
	public function dellink() {
		$id = I('get.id');
		if (!$id) {
			return false;
		}
		if (I('get.n')) {
			$del = M($this->link_list)->where(array('id' => $id))->delete();
			if ($del) {
				$this->success('删除成功');
			} else {
				$this->error('删除失败');
			}
		}
		$link = M($this->link);
		$map['linkid'] = $id;
		$link_count = $link->where($map)->count();
		if ($link_count > 1) {
			$this->error('请先删除下级子节点后再操作!');
		}

		$del = M($this->link_list)->where(array('id' => $id))->delete();
		if ($del) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}
	}
	//修改联动模型
	public function romlink() {
		$name = I('get.name');
		$val = I('get.val');
		$id = I('get.id');
		$data[$name] = $val;

		if (M($this->link)->where(array('id' => $id))->save($data)) {
			$json['status'] = true;
			$json['content'] = "成功修改为{$val}";
		} else {
			$json['status'] = false;
			$json['content'] = "修改失败!";
		}
		$this->ajaxReturn($json);
	}
	//修改模型
	public function rom() {
		$map['id'] = I('get.id', '', 'intval');
		if (IS_POST) {
			$map['id'] = I('post.id', '', 'intval');
			$save = I('post.');
			if (!$save['field']) {
				unset($save['field']);
			}
			if (M('model')->where($map)->save($save)) {
				$this->success('修改成功', U('index'));
			} else {
				$this->error('修改失败');
			}
		} else {
			$this->field = GetModelNan($map['id'], true);
			$this->data = M('model')->where($map)->find();
			$this->display();
		}

	}
	public function linktable() {

		if (IS_POST) {
			$id = I('get.id');
			$mid = I('get.mid');
			$selected = I('get.selected');
			$info = M(GetModelTable($mid))->where(array('id' => $id))->find();
			if ($info) {
				$json['status'] = 1;
				$json['url'] = false;
				$json['data'] = $info;
				$json['info'] = '选择成功';
				$json['field'] = $info[$selected];
				$this->ajaxReturn($json);

			} else {
				$json['status'] = 0;
				$json['info'] = '选择操作失败';
				$this->ajaxReturn($json);
			}
		} else {

			$number = I('get.mid', '', 'intval');
			//0为可删除模型
			$this->where = array('static' => 0);
			//添置按钮
			$array[] = array('title' => '添加', 'class' => 'primary', 'type' => 'button', 'target' => 'myModal');
			$this->assign('add_button', add_button($array));

			//获取模型表单数据
			$this->FormHtml($number);
			$type[] = array('title' => '选择', 'url' => 'linktable', 'class' => 'default', 'type' => 'button');
			$this->assign('array', $type);

			$this->assign('FormConfig', array('title' => '添加', 'url' => U('addmodel', array('mid' => $number))));
			$this->display();

		}
	}
	//新增数据
	public function addmodel() {
		if (IS_POST) {
			$mid = I('get.mid', '', 'intval');
			if (!$mid) {
				return false;
			}
			$this->PublicDataModel($mid);
		}
	}
}
